package cn.psvmc.cxzapi.controller.market;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.market.THotword;
import cn.psvmc.cxzapi.response.PageResult2;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.market.THotwordService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 平台-搜索热词管理
 * @Author auto
 * @Date 2024-08-13
 */
@RestController
@RequestMapping("/hotword")
public class THotwordController {

    @Resource
    private THotwordService tHotwordService;

    /**
    * @Author auto 2024-08-13 新增热词表
    * @Param tHotword 热词表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody THotword tHotword) {
        ToolUtils.validation(new Object[]{tHotword.getHotword(), "热词", 2});
        int addResult = tHotwordService.add(tHotword);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-13 更新热词表
    * @Param tHotword 热词表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody THotword tHotword) {
        ToolUtils.validation(new Object[]{tHotword.getHid(), "id", 1, tHotword.getRversion(), "版本", 1});
        int updateResult = tHotwordService.edit(tHotword);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-13 删除热词表
    * @Param [entity] 删除实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tHotwordService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-13 查询热词表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<THotword> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tHotwordService.list(queryPage, entity));
    }

    /**
     * @Description 搜索列表
     * @Date 2024/8/19 17:13
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public Result listForSel(@RequestBody SearchEntity entity) {
        String name = entity.getName();
        return ResultGenerator.genSuccessResult("查询成功！", tHotwordService.listForSel(name));
    }

    /**
     * @Description 使用热词
     * @Date 2024/8/19 17:18
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/used")
    public Result used(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "热词id", 1});
        tHotwordService.used(id);
        return ResultGenerator.genSuccessResult("更新成功！");
    }
}
